import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: '展示板',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '展示板', icon: 'dashboard' }
    }]
  },
  {
    path: '/system',
    component: Layout,
    redirect: '/system/sys-user',
    name: '系统管理',
    meta: { title: '系统管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'sys-user',
        name: '用户管理',
        component: () => import('@/views/sysuser/index'),
        meta: { title: '用户管理', icon: 'table' }
      },
      {
        path: 'dept',
        name: '部门管理',
        component: () => import('@/views/dept/index'),
        meta: { title: '部门管理', icon: 'table' }
      },
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

router.beforeEach((to, from, next) => {
  if('/login' === to.path || Vue.$cookies.get('access_token')) {
    next()
  }else {
    next('/login')
  }
})

export default router
